package com.hs.li.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.li.pojo.CarManagement;
import com.hs.li.service.ICarManagementService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 汽车管理 前端控制器
 * </p>
 *
 * @author lhj
 * @since 2022-06-13
 */
@CrossOrigin
@RestController
@RequestMapping("/carManagement")
@Slf4j
public class CarManagementController {

    @Autowired
    private ICarManagementService carManagementService;

    @RequestMapping("/changeCarType")
    public ResultBean changeCarTypeByOrderId(String orderId) {
        boolean b = carManagementService.changeCarTypeByOrderId(orderId);

        if (b) {
            return ResultBean.OK("更新车辆状态成功！");
        }
        return ResultBean.ERROR("更新车辆状态失败！");
    }


    /**
     * 分页
     * 有String carType,String carPlate参数时，条件查询
     * 没有条件查询所有
     *
     * @param currentPage
     * @param pageSize
     * @param typeId      车辆类型id
     * @param carPlate    车牌号码
     * @return
     */
    @GetMapping
    public ResultBean queryCarManagement(Integer currentPage, Integer pageSize, Integer typeId, String carPlate) {
        System.out.println("typeId = " + typeId);
        //创建分页对象
        IPage page = new Page(currentPage, pageSize);

        LambdaQueryWrapper<CarManagement> carManagementLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (typeId != null) {
            carManagementLambdaQueryWrapper.eq(CarManagement::getTypeId, typeId).or();
        }

        if (StringUtils.hasText(carPlate)) {
            carManagementLambdaQueryWrapper.eq(CarManagement::getCarPlate, carPlate);
        }

        //调用分页方法
        IPage p = carManagementService.page(page, carManagementLambdaQueryWrapper);

        //获取查询数据和总页数
        List records = p.getRecords();
        Long total = p.getTotal();

        //封装返回数据
        PageBean<CarManagement> pageBean = new PageBean<>();
        pageBean.setList(records);
        pageBean.setTotal(total.intValue());

        if (!CollectionUtils.isEmpty(records)) {

            ResultBean resultBean = new ResultBean();
            resultBean.setMsg("查询成功！");
            resultBean.setData(pageBean);
            resultBean.setCode(Constants.SUCCESS_CODE);
            return resultBean;
        }

        return ResultBean.ERROR();
    }

    @GetMapping("/condition")
    public ResultBean queryByCondition(Integer currentPage, Integer pageSize, String condition, Integer typeId) {

        List<CarManagement> carManagements = carManagementService.searchCarManagementByCondition(currentPage, pageSize, condition, typeId);

        ResultBean resultBean = new ResultBean();
        resultBean.setData(carManagements);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("查询成功！");

        return resultBean;
    }

    /**
     * 添加功能
     * 给车辆状态、车辆编号、逻辑删除字段设置默认值
     *
     * @param carManagement
     * @return
     */
    @PostMapping
    public ResultBean addCarManagement(@RequestBody CarManagement carManagement) {
        //设置默认状态为 0（在库）
        carManagement.setCarStatus(0);
        //查询最后一条数据中的carNumber++
        Integer carNumber = carManagementService.searchLastCarNumber();
        carManagement.setCarNumber(++carNumber);
        //设置逻辑删除默认状态
        carManagement.setDeleted(0);

        boolean b = carManagementService.save(carManagement);

        if (b) {
            return ResultBean.OK("添加成功！");
        }
        return ResultBean.ERROR("添加失败！");
    }

    /**
     * 根据id进行逻辑删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResultBean deleteCarManagement(@PathVariable Integer id) {
        boolean b = carManagementService.removeById(id);

        if (b) {
            return ResultBean.OK("删除成功！");
        }
        return ResultBean.ERROR("删除失败！");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultBean findCarManagementById(@PathVariable Integer id) {
        CarManagement carManagement = carManagementService.getById(id);

        ResultBean resultBean = new ResultBean();
        resultBean.setData(carManagement);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("查询成功！");

        return resultBean;
    }

    @PutMapping
    public ResultBean updateCarManagement(@RequestBody CarManagement carManagement) {
        boolean b = carManagementService.updateById(carManagement);

        if (b) {
            return ResultBean.OK("更新成功！");
        }
        return ResultBean.ERROR("更新失败！");

    }

    @RequestMapping("/search")
    public ResultBean searchCarManagementBycarrierNumber(String carrierName) {

        if (StringUtils.hasText(carrierName)) {
            List<CarManagement> list = carManagementService.searchCarManagementByCarrierName(carrierName);

            ResultBean resultBean = new ResultBean();
            resultBean.setData(list);
            resultBean.setCode(Constants.SUCCESS_CODE);
            resultBean.setMsg("查询车辆信息成功！");
            return resultBean;
        }
        return ResultBean.ERROR("没有查询到车辆信息！");
    }
}
